﻿using HostDemo;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Quartz;
using Quartz.Impl.Matchers;
using Quartz.Job;
using Quartz.Spi;
using Share;

// 1. Install-package Quartz.AspNetCore

var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddQuartz(q =>
{
    // 2. 添加job
    q.AddJob<SampleJob>(SampleJob.Key, job =>
    {
        job.UsingJobData("greeting", "Hello")
        .UsingJobData("user", "fan");
    });

    // 3. 添加 trigger
    q.AddTrigger(trigger =>
    {
        trigger.ForJob(SampleJob.Key)
            .WithIdentity("trigger", "group1")
            .StartNow()
            .WithSimpleSchedule(scheduler => scheduler.WithIntervalInSeconds(5).RepeatForever());
    });

    // 4. 添加 JobListener 监听指定JobKey
    //q.AddJobListener<SampleJobListener>(KeyMatcher<JobKey>.KeyEquals(SampleJob.Key));
    // 监听一组 JobGroup
    q.AddJobListener<SampleJobListener>(GroupMatcher<JobKey>.GroupEquals(SampleJob.Key.Group));

    // 5. 添加 triggerListener
    q.AddTriggerListener<SampleTriggerListener>();
});

//builder.Services.AddQuartzHostedService();
builder.Services.AddQuartzHostedService(options => options.WaitForJobsToComplete = true);

var app = builder.Build();

app.Run();